package com.iut.sujet1.manager;

import java.util.ArrayList;

import com.iut.lib.OtherSort;
import com.iut.sujet1.metier.Carnet;
import com.iut.sujet1.metier.Personne;

//import iut.lib.MySort;

public class CarnetManager implements CarnetPers {

	private Carnet c;
	
	public CarnetManager(){
		c = new Carnet();
	}
	public CarnetManager(Carnet c){
		setC(c);
	}
	

	public void AjouterPersonne(Personne pers){
		
Personne tab[] = c.getTab();
		
		if(c.getNbE() <= tab.length){
			tab[c.getNbE()] = pers;
			c.setNbE(c.getNbE()+1);
		}
		
	}
	public void SupprimerPersonne(int id){
		Personne tab[] = c.getTab();
		if(c.getNbE() > 0 && id >= 0 && id <= c.getNbE()){
			tab[id] = tab[c.getNbE()-1];
			c.setNbE(c.getNbE()-1);
		}
		
	}
	public ArrayList<Personne> RechercherPersonneByNom(String nom){
		Personne tab[] = c.getTab();
		Personne pers;
		ArrayList<Personne> list = new ArrayList<Personne>();
		for (int i = 0; i < c.getNbE(); i++) {
			if(tab[i].getNom().toUpperCase().equals(nom.toUpperCase())){
				pers = tab[i];
				pers.setId(i);
				list.add(pers);
			}
		}
		return list;
	}
	public void TrierCarnet(){
		//MySort.quickSort(c.getTab(), 0, c.getNbE()-1);
		OtherSort.quickSort(c.getTab(), 0, c.getNbE()-1);
	}
	/**
	 * @return the c
	 */
	public Carnet getC() {
		return c;
	}
	/**
	 * @param c the c to set
	 */
	public void setC(Carnet c) {
		this.c = c;
	}
	
}
